﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CCU.Controls
{
    /// <summary>
    /// 
    /// </summary>
    public partial class TextBoxRequired : TextBox
    {
        #region Fields

        const string DefaultRequiredMessage = "This field is required";

        private Font rowFont = null;
        private string requiredMessage = DefaultRequiredMessage;
        private Font tequiredMessageFont = new Font("Segoe UI", 8.25F, FontStyle.Italic, GraphicsUnit.Point, ((byte)(0)));

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the current text in the TextboxRequired.
        /// </summary>
        /// <value>The text displayed in the control.</value>
        public virtual string RequiredMessage
        {
            get
            {
                return requiredMessage;
            }
            set
            {
                requiredMessage = value ?? DefaultRequiredMessage;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public virtual Font RequiredMessageFont
        {
            get
            {
                return tequiredMessageFont;
            }
            set
            {
                tequiredMessageFont = value ?? this.Font;
            }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// 
        /// </summary>
        public TextBoxRequired()
        {
            InitializeComponent();

            GotFocus += new EventHandler(TextBoxRequired_OnGotFocus);
            LostFocus += new EventHandler(TextBoxRequired_OnLostFocus);
            TextChanged += new EventHandler(TextBoxRequired_OnTextChanged);
            ParentChanged += new EventHandler(TextBoxRequired_OnParentChanged);
        }

        #endregion

        #region Methods

        /// <summary>
        /// 
        /// </summary>
        /// <param name="container"></param>
        /// <returns></returns>
        public static bool IsAllTextBoxRequiredSet(Control container)
        {
            if (container == null)
                return true;

            int count = 0;
            GetAllTextBoxRequiredEmptyCount(container, ref count);

            return (count == 0);
        }

        private static void GetAllTextBoxRequiredEmptyCount(Control container, ref int count)
        {
            foreach (Control control in container.Controls)
            {
                if (control is TextBoxRequired)
                {
                    TextBoxRequired textbox = control as TextBoxRequired;
                    if (textbox.Text == string.Empty)
                    {
                        textbox.Text = textbox.RequiredMessage;
                        textbox.Font = new System.Drawing.Font("Segoe UI", 8.25F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                    }
                    if (textbox.Text == textbox.RequiredMessage)
                    {
                        count++;
                    }
                }
                else
                {
                    GetAllTextBoxRequiredEmptyCount(control, ref count);
                }
            }
        }

        #endregion

        #region Events

        private void TextBoxRequired_OnGotFocus(object sender, EventArgs e)
        {
            if (Text == RequiredMessage)
            {
                Text = string.Empty;
            }
        }

        private void TextBoxRequired_OnLostFocus(object sender, EventArgs e)
        {
            if (Text == string.Empty)
            {
                rowFont = Font;
                Text = RequiredMessage;
                Font = RequiredMessageFont;
            }
        }

        private void TextBoxRequired_OnTextChanged(object sender, EventArgs e)
        {
            if (Text != RequiredMessage)
            {
                Font = rowFont;
            }
        }

        private void TextBoxRequired_OnParentChanged(object sender, EventArgs e)
        {
            TextBoxRequired_OnLostFocus(sender, e);
        }

        #endregion
    }
}
